added SSCLI 1.0
[windows-sources.git] / shared source / wpf / src / host / shimimpl / classfactory.cxx
blob9f257761eb5ff41b566aadef4327efb35525d108
1 //+-----------------------------------------------------------------------
2 //
3 // Copyright (c) Microsoft Corporation. All rights reserved.
4 //
5 // Description:
6 // Implements the ClassFactory of PresentationHost.
7 //
8 // History:
9 // 2002/06/12-murrayw
10 // Created
11 // 2007/09/20-[....]
12 // Ported Windows->DevDiv. See SourcesHistory.txt.
14 //------------------------------------------------------------------------
16 #include "Precompiled.hxx"
17 #include "ClassFactory.hxx"
18 #include "HostShim.hxx"
19 #include "Main.hxx"
21 DWORD CClassFactory::s_dwRegistrationToken[] = {0,0};
22 LPCLSID CClassFactory::s_clsIdActivated = NULL;
24 //******************************************************************************
26 // CClassFactory::CClassFactory
28 //******************************************************************************
30 CClassFactory::CClassFactory(__in REFCLSID clsId)
31 : m_clsId(clsId)
33 m_ObjRefCount = 0;
36 //******************************************************************************
38 // CClassFactory::~CClassFactory
40 //******************************************************************************
42 CClassFactory::~CClassFactory()
47 //******************************************************************************
49 // CClassFactory::RegisterClassObjects
51 // This method creates ClassFactory objects and registers their objects based
52 // on the flags. We don't hold on to the factories; OLE has them, and when
53 // we Revoke them, OLE will Release them and they will be deleted.
54 //******************************************************************************
56 HRESULT CClassFactory::RegisterClassObjects()
58 HRESULT hr = S_OK;
60 ASSERT(s_dwRegistrationToken[0] == 0);
61 ASSERT(s_dwRegistrationToken[1] == 0);
63 #ifdef DOCUMENT_SHIM
64 CClassFactory* pFactory = new CClassFactory(CLSID_DocObjPackage);
65 CK_ALLOC(pFactory);
66 CKHR(pFactory->RegisterClassObject(s_dwRegistrationToken[0]));
67 #endif
69 #ifdef APPLICATION_SHIM
70 CClassFactory* pFactory = new CClassFactory(CLSID_DocObjXaml);
71 CK_ALLOC(pFactory);
72 CKHR(pFactory->RegisterClassObject(s_dwRegistrationToken[0]));
74 pFactory = new CClassFactory(CLSID_DocObjXapp);
75 CK_ALLOC(pFactory);
76 CKHR(pFactory->RegisterClassObject(s_dwRegistrationToken[1]));
77 #endif
79 Cleanup:
80 if (hr != S_OK)
82 CClassFactory::RevokeAllClassObjects();
85 return hr;
88 //******************************************************************************
90 // CClassFactory::RevokeAllClassObjects
92 // Revoke any registered ClassObjects, and delete the ClassFactories.
93 // This doesn't check any return values, nor does it return anything,
94 // because this is a cleanup step, and if it fails, it really isn't
95 // actionable.
96 //******************************************************************************
98 void CClassFactory::RevokeAllClassObjects()
100 for (UINT tokenIndex = 0; tokenIndex < MAX_TOKENS; ++tokenIndex)
102 if (s_dwRegistrationToken[tokenIndex])
104 CoRevokeClassObject(s_dwRegistrationToken[tokenIndex]);
105 s_dwRegistrationToken[tokenIndex] = 0;
110 //******************************************************************************
112 // CClassFactory::RegisterClassObject
114 // This method registers the ClsObject that this is offering.
115 //******************************************************************************
117 HRESULT CClassFactory::RegisterClassObject(DWORD& dwRegistrationToken)
119 HRESULT hr = S_OK;
121 LPUNKNOWN pUnknown = reinterpret_cast<LPUNKNOWN> (this);
123 //register the MIME-type handler class object
124 CKHR(CoRegisterClassObject(m_clsId,
125 pUnknown,
126 CLSCTX_LOCAL_SERVER,
127 REGCLS_SINGLEUSE,
128 &dwRegistrationToken));
129 Cleanup:
130 if (hr != S_OK && dwRegistrationToken)
132 CoRevokeClassObject(dwRegistrationToken);
133 dwRegistrationToken = 0;
135 return hr;
138 //******************************************************************************
140 // CClassFactory::QueryInterface
142 //******************************************************************************
144 STDMETHODIMP CClassFactory::QueryInterface(REFIID riid,
145 __out LPVOID *ppReturn)
147 HRESULT hr = S_OK;
149 CK_PARG(ppReturn);
151 if (IsEqualIID(riid, IID_IUnknown))
153 *ppReturn = reinterpret_cast<LPUNKNOWN>
154 (reinterpret_cast<LPCLASSFACTORY>(this));
156 m_ObjRefCount++;
158 else if (IsEqualIID(riid, IID_IClassFactory))
160 *ppReturn = reinterpret_cast<LPCLASSFACTORY>(this);
161 m_ObjRefCount++;
163 else
165 *ppReturn = NULL;
166 hr = E_NOINTERFACE;
169 Cleanup:
171 return hr;
174 //******************************************************************************
176 // CClassFactory::AddRef
178 //******************************************************************************
180 STDMETHODIMP_(DWORD) CClassFactory::AddRef()
182 return ++m_ObjRefCount;
185 //******************************************************************************
187 // CClassFactory::Release
189 //******************************************************************************
191 STDMETHODIMP_(DWORD) CClassFactory::Release()
193 if (--m_ObjRefCount == 0)
195 delete this;
196 return 0;
199 return m_ObjRefCount;
202 //******************************************************************************
204 // CClassFactory::CreateInstance
206 //******************************************************************************
208 STDMETHODIMP CClassFactory::CreateInstance(__in LPUNKNOWN pUnknown,
209 REFIID riid,
210 __out LPVOID *ppObject)
212 EventWriteWpfHostUm_ClassFactoryCreateInstance();
214 HRESULT hr = S_OK;
215 CHostShim * pHostShim = NULL;
217 CK_PARG(ppObject);
219 *ppObject = NULL;
221 if (pUnknown != NULL)
223 hr = CLASS_E_NOAGGREGATION;
224 goto Cleanup;
227 pHostShim = new CHostShim();
228 CK_ALLOC(pHostShim);
230 CKHR(pHostShim->QueryInterface(riid, ppObject));
232 SetActivatedClsId(m_clsId);
234 Cleanup:
236 if (FAILED(hr))
238 DEL_PTR(pHostShim);
241 // Revoke any class factories that may have been created.
242 // Current implementation only creates one.
243 CClassFactory::RevokeAllClassObjects();
245 return hr;
248 //******************************************************************************
250 // CClassFactory::LockServer
252 //******************************************************************************
254 STDMETHODIMP CClassFactory::LockServer(BOOL fLock)
256 HRESULT hr = S_OK;
258 if (fLock)
260 InterlockedIncrement(&g_ServerRefCount);
262 else
264 InterlockedDecrement(&g_ServerRefCount);
266 if (g_ServerRefCount <= 0)
268 PostQuitMessage(ERROR_SUCCESS);
272 return hr;